package com.purong.purong.widget;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;


import com.purong.purong.R;
import com.purong.purong.adapter.CreditSearchMoreAdapter;
import com.purong.purong.adapter.CreditSearchMoreColorAdapter;
import com.purong.purong.bean.CreditSearchBean.SearchType.SearchItem;
import com.purong.purong.util.CommonUtil;
import com.zhy.view.flowlayout.TagFlowLayout;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by Administrator on 2016/8/4.
 */
public class LoanSearchMoreDialog extends Dialog{
    private Context mContext;
    private OnSearchChoseListener mListener;
    private ArrayList<SearchItem> mItemList =new ArrayList<>();
    private LinearLayout specLay;
    private CreditSearchMoreColorAdapter colorAdapter;
    private List<CreditSearchMoreAdapter> adapterList=new ArrayList<>();
    public LoanSearchMoreDialog(Context context, ArrayList<SearchItem> itemList, OnSearchChoseListener listenr) {
        super(context, R.style.DialogTranBgDimEnabled);
        this.mContext=context;
        if (itemList!=null)
        this.mListener=listenr;
        this.mItemList =itemList;
        setCanceledOnTouchOutside(true);
        Window window = this.getWindow();
        window .setGravity(Gravity.LEFT | Gravity.TOP);
        WindowManager.LayoutParams lp = window.getAttributes();
        int[] location=new int[2];
//        v.getLocationOnScreen(location);
//        Log.e("zwj","x="+location[0]);
        lp.x = 0;
        lp.y = CommonUtil.dip2px(context,95);


        window .setAttributes(lp);
    }

    @Override
    public void dismiss() {
        super.dismiss();
        if (mListener!=null){
            mListener.onDismiss();
        }
    }
    private void notifyData(){
        if (colorAdapter!=null){
            colorAdapter.notifyDataChanged();
        }
        if (adapterList!=null){
            for (int i = 0; i < adapterList.size(); i++) {
                if (adapterList.get(i)!=null)
                adapterList.get(i).notifyDataChanged();
            }
        }
    }
    @Override
    public void show() {
        super.show();
        if (mListener!=null){
            mListener.onShow();
        }
        for (int i = 0; i <mItemList.size() ; i++) {
            for (int j = 0; j <mItemList.get(i).getValue().size() ; j++){
                mItemList.get(i).getValue().get(j).setCheckTmp(mItemList.get(i).getValue().get(j).isCheck());
            }
        }
        notifyData();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        View inflate = LayoutInflater.from(mContext).inflate(R.layout.dialog_search_more_bottom, null);
        setContentView(inflate);
        findViewById(R.id.main_lay).setLayoutParams(new FrameLayout.LayoutParams(CommonUtil.getScreenWidth(mContext), CommonUtil.getScreenHeight(mContext)-CommonUtil.getStatusBarHeight(mContext)-CommonUtil.dip2px(mContext,95)));
        findViewById(R.id.main_lay).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                LoanSearchMoreDialog.this.dismiss();
            }
        });
        specLay=(LinearLayout) findViewById(R.id.spec_lay);
        for (int i = 0; i <mItemList.size() ; i++) {

                View specView= LayoutInflater.from(mContext).inflate(R.layout.more_item_lay,null);
                TextView specName=(TextView)specView.findViewById(R.id.spec_name);
                specName.setText(mItemList.get(i).getName());
                TagFlowLayout mflowLayout=(TagFlowLayout)specView.findViewById(R.id.flowlayout);
                if (mItemList.get(i).getFiled().equals("credit_color")){
                    colorAdapter=new CreditSearchMoreColorAdapter(mContext,mflowLayout,mItemList.get(i).getValue());
                    mflowLayout.setAdapter(colorAdapter);
                }else {
                    CreditSearchMoreAdapter adapter=new CreditSearchMoreAdapter(mContext,mflowLayout,mItemList.get(i).getValue());
                    mflowLayout.setAdapter(adapter);
                    adapterList.add(adapter);
                }
                specLay.addView(specView);
            }
        findViewById(R.id.reset).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                for (int i = 0; i <mItemList.size() ; i++) {
                    for (int j = 0; j <mItemList.get(i).getValue().size() ; j++){
                        mItemList.get(i).getValue().get(j).setCheckTmp(false);
                    }
                }
                notifyData();
            }
        });
        findViewById(R.id.confirm).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                for (int i = 0; i <mItemList.size() ; i++) {
                    for (int j = 0; j <mItemList.get(i).getValue().size() ; j++){
                        mItemList.get(i).getValue().get(j).setCheck(mItemList.get(i).getValue().get(j).isCheckTmp());
                    }
                }
                if (mListener!=null){
                    mListener.onConfirm();
                }
                dismiss();
            }
        });



    }

    public interface OnSearchChoseListener{
        void onConfirm();
        void onShow();
        void onDismiss();
    }
}
